package com.liar.modular.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liar.modular.user.model.UserInfo;
import com.liar.modular.user.dao.UserInfoMapper;
import com.liar.modular.user.model.QueryInfo;
import com.liar.utils.common.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoService extends ServiceImpl<UserInfoMapper, UserInfo> {

    @Autowired
    private UserInfoMapper infoMapper;

     public UserInfo login(UserInfo userInfo){

         UserInfo username = infoMapper.selectOne(new QueryWrapper<UserInfo>().eq("username", userInfo.getUsername()));
         if(EmptyUtil.isNotEmpty(username)&&userInfo.getPassword().equals(username.getPassword())){
             return username;
         }else {
             return null;
         }
     }
     public Page<UserInfo> userList(QueryInfo queryInfo){
         Page<UserInfo> page = new Page<>();
         page.setCurrent(queryInfo.getPagenum());
         page.setSize(queryInfo.getPagesize());
         Page<UserInfo> username = infoMapper.selectPage(page, null );//new QueryWrapper<UserInfo>().like("username", queryInfo.getQuery())
         return username;
     }
     /**
      * 用户状态修改
     * */
     public boolean state(UserInfo userInfo){
         UserInfo userInfo1 = infoMapper.selectById(userInfo.getId());
         if(EmptyUtil.isNotEmpty(userInfo1)){
             UserInfo ui = new UserInfo();
             ui.setId(userInfo.getId());
             ui.setState(userInfo.isState());
             int i = infoMapper.updateById(ui);
             if(i>0){
                 return  true;
             }
         }
         return false;
     }
    /**
     * 添加用户
     */
    public boolean addUser(UserInfo userInfo){
        UserInfo username = infoMapper.selectOne(new QueryWrapper<UserInfo>().eq("username", userInfo.getUsername()));
        if(EmptyUtil.isNotEmpty(username)){
            return false;
        }
        userInfo.setState(true);
        userInfo.setId(IdWorker.getIdStr());
        userInfo.setRole("普通用户");
        int insert = infoMapper.insert(userInfo);
        if(insert>0){
            return true;
        }
        return false;
    }
}
